<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>转账 - 银行客户系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        body {
            background-color: #f5f7fa;
            color: #333;
            overflow-x: hidden;
        }

        .app-container {
            display: flex;
            min-height: 100vh;
            width: 100%;
        }

        .sidebar {
            width: 250px;
            background: linear-gradient(to bottom, #1a3a6c, #2c5a8d);
            color: #fff;
            padding: 30px 0;
            flex-shrink: 0;
        }

        .logo-container {
            padding: 0 25px 25px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .logo-icon {
            width: 50px;
            height: 50px;
            background-color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: #1a3a6c;
            font-weight: bold;
        }

        .logo-text {
            font-size: 20px;
            font-weight: 600;
        }

        .user-info {
            display: flex;
            align-items: center;
            padding: 20px 25px;
            gap: 15px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .avatar {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: #ffcc00;
            color: #1a3a6c;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 20px;
        }

        .user-details {
            flex: 1;
        }

        .user-name {
            font-weight: 600;
            margin-bottom: 5px;
            font-size: 16px;
        }

        .user-account {
            font-size: 13px;
            opacity: 0.8;
        }

        .sidebar-menu {
            padding: 0 15px;
        }

        .menu-item {
            padding: 14px 20px;
            display: flex;
            align-items: center;
            gap: 15px;
            cursor: pointer;
            transition: all 0.2s;
            border-radius: 8px;
            margin-bottom: 8px;
        }

        .menu-item:hover {
            background: rgba(255, 255, 255, 0.1);
        }

        .menu-item.active {
            background: rgba(255, 255, 255, 0.15);
            border-left: 4px solid #ffcc00;
        }

        .menu-item i {
            font-size: 18px;
            width: 24px;
            text-align: center;
        }

        .menu-text {
            font-size: 15px;
        }

        /* 右侧内容区样式 */
        .main-content {
            flex: 1;
            display: flex;
            flex-direction: column;
            overflow: auto;
        }

        .topbar {
            background-color: white;
            padding: 0 30px;
            height: 80px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            border-bottom: 1px solid #eee;
            flex-shrink: 0;
        }

        .page-title {
            font-size: 24px;
            font-weight: 600;
            color: #1a3a6c;
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .page-title i {
            background-color: #1a3a6c;
            color: white;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
        }

        .current-time {
            font-size: 14px;
            color: #666;
            font-weight: 500;
        }

        .content-area {
            flex: 1;
            padding: 30px;
            overflow-y: auto;
            background-color: #f8fafc;
            display: flex;
            justify-content: center;
            align-items: flex-start;
        }

        .withdraw-form-section {
            width: 100%;
            max-width: 800px;
        }

        .section-title {
            font-size: 20px;
            color: #1a3a6c;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #1a3a6c;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .section-title i {
            color: #1a3a6c;
        }

        .withdraw-form-container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            padding: 30px;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-group label {
            display: block;
            margin-bottom: 10px;
            font-weight: 600;
            color: #444;
            font-size: 16px;
        }

        .account-display {
            padding: 15px;
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            color: #1a3a6c;
        }

        .account-display .account-number {
            font-weight: 600;
            letter-spacing: 1px;
        }

        .form-group input {
            width: 100%;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            border-color: #1a3a6c;
            box-shadow: 0 0 0 3px rgba(26, 58, 108, 0.1);
            outline: none;
        }

        .amount-input {
            position: relative;
        }

        .currency-symbol {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 24px;
            font-weight: 600;
            color: #1a3a6c;
        }

        .amount-input input {
            padding-left: 45px;
            font-size: 24px;
            font-weight: 600;
            height: 70px;
        }

        .quick-amounts {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 15px;
            margin-top: 15px;
        }

        .amount-btn {
            background-color: #f0f4f9;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 12px;
            text-align: center;
            font-weight: 600;
            color: #444;
            cursor: pointer;
            transition: all 0.2s;
        }

        .amount-btn:hover {
            background-color: #e0e8f0;
            border-color: #1a3a6c;
            color: #1a3a6c;
        }

        .btn-group {
            display: flex;
            gap: 15px;
            margin-top: 30px;
        }

        .btn {
            flex: 1;
            padding: 16px;
            border-radius: 8px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            border: none;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .btn-primary {
            background-color: #1a3a6c;
            color: white;
        }

        .btn-primary:hover {
            background-color: #15315a;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(26, 58, 108, 0.3);
        }

        .btn-outline {
            background-color: transparent;
            border: 2px solid #1a3a6c;
            color: #1a3a6c;
        }

        .btn-outline:hover {
            background-color: #f0f4f9;
        }

        .withdraw-success {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.7);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s ease;
        }

        .withdraw-success.show {
            opacity: 1;
            visibility: visible;
        }

        .success-content {
            background: white;
            padding: 30px;
            border-radius: 15px;
            text-align: center;
            max-width: 500px;
            transform: translateY(20px);
            transition: transform 0.3s ease;
        }

        .withdraw-success.show .success-content {
            transform: translateY(0);
        }

        .success-content i {
            font-size: 60px;
            color: #2ecc71;
            margin-bottom: 20px;
        }

        .success-content h2 {
            color: #1a3a6c;
            margin-bottom: 20px;
        }

        .transaction-details {
            text-align: left;
            margin-bottom: 25px;
        }

        .transaction-details p {
            margin-bottom: 10px;
        }

        /* 响应式设计 */
        @media (max-width: 900px) {
            .app-container {
                flex-direction: column;
            }

            .sidebar {
                width: 100%;
            }
        }

        @media (max-width: 576px) {
            .topbar {
                padding: 0 20px;
                height: 70px;
                flex-direction: column;
                justify-content: center;
                gap: 5px;
            }

            .page-title {
                font-size: 20px;
            }

            .content-area {
                padding: 20px;
            }

            .quick-amounts {
                grid-template-columns: repeat(2, 1fr);
            }

            .success-content {
                padding: 20px;
                margin: 0 20px;
            }
        }
    </style>
</head>
<body>
    <div class="app-container">
        <!-- 左侧菜单 -->
        <div class="sidebar">
            <div class="logo-container">
                <div class="logo">
                    <div class="logo-icon">
                        <i class="fas fa-university"></i>
                    </div>
                    <div class="logo-text">银行系统</div>
                </div>
            </div>

            <div class="user-info">
                <div class="avatar" id="user-avatar"></div>
                <div class="user-details">
                    <div class="user-name" id="user-name"></div>
                    <div class="user-account" id="user-account"></div>
                </div>
            </div>
            <div class="sidebar-menu">
                <div class="menu-item" onclick="location.href='../user_home.html';">
                    <i class="fas fa-wallet"></i>
                    <div class="menu-text">账户信息</div>
                </div>
                <div class="menu-item" onclick="location.href='Deposit.html';">
                    <i class="fas fa-money-bill-wave"></i>
                    <div class="menu-text">存款</div>
                </div>
                <div class="menu-item active">
                    <i class="fas fa-hand-holding-usd"></i>
                    <div class="menu-text">取款</div>
                </div>
                <div class="menu-item" onclick="location.href='Transfer.html';">
                    <i class="fas fa-exchange-alt"></i>
                    <div class="menu-text">转账</div>
                </div>
                <div class="menu-item" onclick="location.href='AccountLost.html';">
                    <i class="fas fa-lock"></i>
                    <div class="menu-text">账户挂失</div>
                </div>
                <div class="menu-item" onclick="location.href='ChangePassword.html';">
                    <i class="fas fa-key"></i>
                    <div class="menu-text">修改密码</div>
                </div>
                <div class="menu-item" onclick="location.href='TransactionRecords.html';">
                    <i class="fas fa-history"></i>
                    <div class="menu-text">交易记录</div>
                </div>
                <div class="menu-item" id="logout-btn">
                    <i class="fas fa-sign-out-alt"></i>
                    <div class="menu-text">安全退出</div>
                </div>
            </div>
        </div>

        <!-- 右侧内容区 -->
        <div class="main-content">
            <div class="topbar">
                <div class="page-title">
                    <i class="fas fa-hand-holding-usd"></i>
                    <span>取款服务</span>
                </div>
                <div class="current-time" id="current-time"></div>
            </div>

            <div class="content-area">
                <div class="withdraw-form-section">
                    <h2 class="section-title">
                        <i class="fas fa-piggy-bank"></i>取款操作
                    </h2>

                    <div class="withdraw-form-container">
                        <div class="form-group">
                            <label>取款账户</label>
                            <div class="account-display">
                                <i class="fas fa-credit-card"></i>
                                <span class="account-number" id="current-account">
                                </span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="amount">取款金额 (¥)</label>
                            <div class="amount-input">
                                <span class="currency-symbol">¥</span>
                                <input type="number" id="amount" placeholder="请输入取款金额">
                            </div>

                            <div class="quick-amounts">
                                <div class="amount-btn" data-amount="100">¥100</div>
                                <div class="amount-btn" data-amount="500">¥500</div>
                                <div class="amount-btn" data-amount="1000">¥1,000</div>
                                <div class="amount-btn" data-amount="2000">¥2,000</div>
                                <div class="amount-btn" data-amount="5000">¥5,000</div>
                                <div class="amount-btn" data-amount="10000">¥10,000</div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="remark">取款备注 (可选)</label>
                            <input type="text" id="remark" placeholder="请输入取款用途或备注信息">
                        </div>

                        <div class="btn-group">
                            <button class="btn btn-outline">
                                <i class="fas fa-times"></i> 取消
                            </button>
                            <button class="btn btn-primary" id="btn-withdraw">
                                <i class="fas fa-check-circle"></i> 确认取款
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 取款成功弹窗 -->
    <div class="withdraw-success" id="withdraw-success">
        <div class="success-content">
            <i class="fas fa-check-circle"></i>
            <h2>取款成功！</h2>
            <div class="transaction-details">
                <p><strong>账户:</strong> <span id="success-account"></span></p>
                <p><strong>取款金额:</strong> <span id="success-amount"></span></p>
                <p><strong>备注:</strong> <span id="success-remark"></span></p>
                <p><strong>交易时间:</strong> <span id="success-time"></span></p>
                <p><strong>当前余额:</strong> <span id="success-balance"></span></p>
            </div>
            <button id="close-success" class="btn-primary" style="padding: 12px 30px; font-size: 16px; border-radius: 8px; cursor: pointer;">确认</button>
        </div>
    </div>

    <script>
        async function getUserInfo() {
            const cardID = sessionStorage.getItem('cardID');
            const userName = sessionStorage.getItem('userName');
            if (!cardID) {
                alert('请先登录系统');
                window.location.href = '../login.html';
                return null;
            }
            return { cardID, userName };
        }

        function renderUserInfo() {
            const cardID = sessionStorage.getItem('cardID');
            const userName = sessionStorage.getItem('userName');
            if (!cardID) {
                alert('请先登录系统');
                window.location.href = '../login.html';
                return;
            }

            const formattedCardID = cardID.replace(/(\d{4})(?=\d)/g, '$1 ');
            document.getElementById('current-account').textContent = formattedCardID;
            document.getElementById('user-account').textContent = `账号: ${formattedCardID}`;
            document.getElementById('user-name').textContent = userName;
            document.getElementById('user-avatar').textContent = userName.charAt(0);
        }

        // 更新时间显示
        function updateTime() {
            const now = new Date();
            const options = {
                year: 'numeric',
                month: 'long',
                day: 'numeric',
                hour: '2-digit',
                minute: '2-digit',
                second: '2-digit',
                hour12: false
            };
            document.getElementById('current-time').textContent = now.toLocaleDateString('zh-CN', options);
        }

        // 初始更新时间
        updateTime();
        // 每秒更新一次时间
        setInterval(updateTime, 1000);

        // 快速金额按钮
        document.querySelectorAll('.amount-btn').forEach(button => {
            button.addEventListener('click', function() {
                const amount = this.getAttribute('data-amount');
                document.getElementById('amount').value = amount;

                // 添加视觉反馈
                this.style.backgroundColor = '#1a3a6c';
                this.style.color = 'white';

                // 1秒后恢复原样
                setTimeout(() => {
                    this.style.backgroundColor = '';
                    this.style.color = '';
                }, 1000);
            });
        });

        // 取款按钮事件
        document.getElementById('btn-withdraw').addEventListener('click', async function() {
            const amount = parseFloat(document.getElementById('amount').value);
            const remark = document.getElementById('remark').value || '无备注';
            const userInfo = await getUserInfo();

            if (!amount || amount <= 0) {
                alert('请输入有效的取款金额');
                return;
            }

            if (amount % 100 !== 0) {
                alert('取款金额必须是100的整数倍');
                return;
            }

            if (amount > 50000) {
                if(!confirm('您正在取出超过50,000元，根据银行规定，大额取款需要额外确认。确定继续吗？')) {
                    return;
                }
            }
            try {
                // 发送取款请求到后端
                const response = await fetch('http://localhost:8080/customer/deleteBalance', {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        cardID: userInfo.cardID,
                        balance: amount
                    })
                });

                const result = await response.json();

                if (response.ok && result.code === '200') {
                    // 取款成功
                    document.getElementById('success-account').textContent =
                        userInfo.cardID.replace(/(\d{4})(?=\d)/g, '$1 ');
                    document.getElementById('success-amount').textContent =
                        `¥${amount.toLocaleString('zh-CN')}`;
                    document.getElementById('success-remark').textContent = remark;
                    document.getElementById('success-time').textContent = new Date().toLocaleString();

                    // 如果后端返回新余额则显示
                    if (result.data && result.data.balance !== undefined) {
                        document.getElementById('success-balance').textContent =
                            `¥${result.data.balance.toLocaleString('zh-CN')}`;
                    } else {
                        document.getElementById('success-balance').textContent = '请查询最新余额';
                    }

                    // 显示成功弹窗
                    const successPopup = document.getElementById('withdraw-success');
                    successPopup.classList.add('show');

                    // 清空表单
                    document.getElementById('amount').value = '';
                    document.getElementById('remark').value = '';
                } else {
                    // 处理错误情况
                    alert(`取款失败: ${result.message || '服务器错误'}`);
                }
            } catch (error) {
                console.error('取款请求出错:', error);
                alert('网络错误，请稍后重试');
            }
        });

        // 退出登录
        document.getElementById('logout-btn').addEventListener('click', function() {
            if(confirm('确定要退出系统吗？')) {
                sessionStorage.removeItem('cardID');
                sessionStorage.removeItem('userName');
                window.location.href = '../login.html';
            }
        });

        document.getElementById('close-success').addEventListener('click', function() {
            document.getElementById('withdraw-success').classList.remove('show');
            location.reload();
        });

        // 初始化页面
        document.addEventListener('DOMContentLoaded', function() {
            renderUserInfo();
        });
    </script>
</body>
</html>